<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-folder-add" @click="goAdd">新增</el-button>
                <el-button type="warning" icon="el-icon-edit" @click="goEdit">修改</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="delAllSelection">删除</el-button>
            </div>
            <!-- 搜索表单 -->
            <el-form :model="query" ref="searchRuleForm" label-width="100px">
                <el-row>
                    <el-col :sm="20" :md="6">
                        <el-form-item label="考核类别:" prop="templateTypeId">
                            <el-select
                                v-model="query.templateTypeId"
                                placeholder="请选择"
                                clearable
                                @focus="dictItemForPerfection('考核类别')"
                            >
                                <el-option
                                    v-for="(item,index) in templateTypeOption"
                                    :key="index"
                                    :label="item.itemName"
                                    :value="item.id"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :sm="20" :md="6">
                        <el-form-item label="考核月份:" prop="kaoheMonth">
                            <el-date-picker
                                v-model="query.kaoheMonth"
                                type="month"
                                placeholder="选择月"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :sm="20" :md="6" align="center">
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            @click="submitForm('searchRuleForm')"
                        >搜索</el-button>
                        <el-button type="info" @click="resetForm('searchRuleForm')">重置</el-button>
                    </el-col>
                </el-row>
            </el-form>
            <div class="table-form-box">
                <h3 class="h3">我的自评列表</h3>
            </div>

            <div class="tree-box">
                <div class="table-box">
                    <table-List
                        ref="tableList"
                        :tableData="tableData"
                        :tableHeader="tableHeader"
                        :loading="loading"
                        :pagination="pagination"
                        @pageChange="pageChange"
                        @selectFun="selectFun"
                        @sortChange="sortChange"
                    ></table-List>
                </div>
            </div>
        </div>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
export default {
    name: 'welfareList',
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            query: {
                sortColumn: '' //排序字段 倒序追加 columnName desc
            },
            //关怀类型
            templateTypeOption: [],

            // 通用表格
            loading: false,
            pagination: {
                pageIndex: 1,
                pageSize: 10,
                pageTotal: 0
            },
            query: {
                // evaluationOtherUserId: '',//被考核人Id
                // evaluationOtherUserName: '',//被考核人
                // evaluationUserId: '',//考核人id
                // evaluationUserName: '',//考核人i
                //kaoheEndMonth: '',//
                //kaoheStartMonth: '',//
                kaoheMonth: '', //考核月份
                selectType: 2, //查询类型:0所有人(自己+下属),1.下属,2.自己
                sortColumn: '', //
                templateTypeId: '' //模板类型Id
            },
            tableData: [
                {
                    createUserId: '',
                    createUserName: '',
                    kaoheMonth: '',
                    templateName: '', //模板名称
                    templateTypeId: '', //模板类型
                    templateTypeName: '', //模板类型名称
                    kaoheTotalPoints: '',
                    selfTotalPoints: '', //自评总得分
                    assessmentPoints: '', //上级评分总分
                    finalyTotalPoints: '', //最终得分 这里需要有一个业务打分公式或者目前暂时就以上级评分为准
                    kaoheItems: [], //考核项JSON,
                    evaluationCommont: '' //考核评语
                }
            ],
            tableHeader: [
                // 表头数据
                { prop: 'createUserName', label: '自评人', minWidth: '100px', sortable: 'custom' },
                {
                    prop: 'templateTypeName',
                    label: '考核类别',
                    sortable: 'custom'
                },
                {
                    prop: 'templateName',
                    label: '考核模板',
                    sortable: 'custom'
                },
                {
                    prop: 'kaoheMonth',
                    label: '考核月份',
                    router: 'assessmentSubDetail',
                    routerId: 'id',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM');
                    },
                    sortable: 'custom'
                },
                {
                    prop: 'kaoheTotalPoints',
                    label: '考核总分',
                    sortable: 'custom'
                },
                {
                    prop: 'selfTotalPoints',
                    label: '自评总分',
                    sortable: 'custom'
                },
                {
                    prop: 'assessmentPoints',
                    label: '上级评分',
                    sortable: 'custom'
                },
                 {
                    prop: 'finalyTotalPoints',
                    label: '最终评分',
                    sortable: 'custom'
                },
                {
                    prop: 'evaluationCommont',
                    label: '考核评语',
                    sortable: 'custom'
                },
                {
                    prop: 'createTime',
                    label: '自评时间',
                    formatData: function(val) {
                        let date = new Date(val);
                        return appMain.formatDate(date, 'yyyy-MM-dd');
                    },
                    sortable: 'custom'
                }
            ],
            multipleSelection: []
        };
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
        // console.log(this.$router, 'router')
    },
    created() {
        this.dictItemForPerfection('考核类别');
        this.getData();
    },
    methods: {
        getData() {
            this.loading = true;
            console.log('getData', this.loading);
            let params = {
                pageNo: this.pagination.pageIndex,
                pageSize: this.pagination.pageSize,
                record: this.query
            };

            appMain
                .assessmentKaoheFindAllPageByDTO(params)
                .then(res => {
                    if (res.data.code === 20000) {
                        //TODO 测试环境使用
                        this.timer = setTimeout(() => {
                            //设置延迟执行
                            this.loading = false;
                        }, 1000);

                        this.tableData = res.data.data.list;
                        this.pagination.pageSize = res.data.data.pageSize;
                        this.pagination.pageIndex = res.data.data.pageNum;
                        this.pagination.pageTotal = res.data.data.total;

                        console.log('getData tableData', this.tableData);
                    } else {
                        this.loading = false;
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.log(err);
                });
        },
        delAllSelection() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //请求接口删除数据
                    this.deleteById(this.multipleSelection[0].id);
                })
                .catch(() => {});
        },
        deleteById(id) {
            appMain
                .assessmentKaoheDelById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.getData();
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        goEdit() {
            if (this.multipleSelection.length < 1) {
                this.$message('请选择一条记录');
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message('只能选择一条记录');
                return;
            }
            this.$router.push({
                path: '/assessmentSelf',
                query: {
                    id: this.multipleSelection[0].id
                }
            });
        },
        goAdd() {
            this.$router.push({
                path: '/assessmentSelf'
            });
        },

        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.pagination = {
                        pageIndex: 1,
                        pageSize: 10,
                        pageTotal: 0
                    };
                    this.getData();
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            console.log('resetForm');
            this.query = {
                kaoheMonth: '', //考核月份
                selectType: 2, //查询类型:0所有人(自己+下属),1.下属,2.自己
                sortColumn: '', //
                templateTypeId: '' //模板类型Id
            };
            this.$refs.tableList.clearSort();
            this.$refs[formName].resetFields();
        },
        /**
         * 下拉框数据start
         */
        dictItemForPerfection(item) {
            appMain.dictItem({ typeName: item }).then(res => {
                if (res.data.code === 20000) {
                    if (item == '考核类别') {
                        this.templateTypeOption = res.data.data;
                    }
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        //通用字典选择
        selectChooseItem(item, id) {
            for (const key in item) {
                if (item[key].id == id) {
                    return item[key];
                }
            }
        },
        /**
         * 下拉框获取end
         */
        //表格
        // 分页导航
        pageChange(data) {
            this.pagination = data.backData;
            this.getData();
        },
        // 组件选择完后把数据传过来
        selectFun(data) {
            this.multipleSelection = data.backData;
        },
        //表格组件返回排序对象
        sortChange(data) {
            let column = data.backData;
            //排序
            if (column.order) {
                //倒序
                if (column.order === 'descending') {
                    this.query.sortColumn = column.prop + ' ' + 'desc';
                } else {
                    this.query.sortColumn = column.prop;
                }
            } else {
                //不排序
                this.query.sortColumn = '';
            }
            this.getData();
        }
    }
};
</script>


<style scoped>
.container {
    box-sizing: border-box;
}
.handle-box {
    margin-bottom: 20px;
    flex-wrap: wrap;
    font-size: 12px;
}
.handle-box > div,
.handle-box > button {
    margin-bottom: 15px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 120px;
    display: inline-block;
}
.tree-box {
    border: 1px solid #ddd;
    margin-top: 20px;
    height: calc(100% - 146px);
}
.table-box {
    height: calc(100% - 72px);
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.ml10 {
    margin-left: 10px;
}
.table-form-box {
    text-align: center;
}

.el-input {
    max-width: 220px;
}
.el-date-editor{
   width: 100%!important;
}
</style>